// pages/defect/adjustment/index.js
import {
  _list,
  _add,
  _update,
  _remove
} from '../../../api/recipe-composition-type'
 
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [],
    _parent: {},
    disabledSwipe: false,
    addParamsShow: false,
    
  },
  toMethod(event) {
    const {
      item
    } = event.currentTarget.dataset;
    const ancestor = encodeURIComponent(JSON.stringify(this.data._parent)),
      father = encodeURIComponent(JSON.stringify(item)) ;

    wx.navigateTo({
      url: './function/index?' + `ancestor=${ancestor}&father=${father}`,
    })
  },
  onSwipeDisabld() {
    // this.setData({
    //   disabledSwipe: true
    // })
  },
  onSwipeEnable() {
    // this.setData({
    //   disabledSwipe: false
    // })
  },
  onEdit(event) {
    const {
      index,
      item
    } = event.currentTarget.dataset;
    const instance = this.selectComponent('#_options');
    instance.open(item,index);
  },
  onAdd() {
    const instance = this.selectComponent('#params');
    instance?.onClear();
     this.onAddParamsShow()
  },
  onBack() {
    wx.navigateBack()
  },
  onDelect(event) {
    const dataSource = this.data.dataSource,
      index = event.currentTarget.dataset.index;
    wx.showModal({
      title: '确定要删除吗？',
      icon: 'none',
      success: res => {
        if (res.confirm) {
          _remove(dataSource[index], () => {
            dataSource.splice(index, 1);
            this.setData({
              dataSource
            })
          })
        }
      }
    })
  },
  fetchData() {
    _list({
      adjustmentGlazeTypeId: this.data._parent['id']
    }, res => {
      this.setData({
        dataSource: res
      })
     
    })
  },
 
  onUpdateParams() {
     this.fetchData();
  },
  onAddParamsCancel() {
    this.setData({
      addParamsShow: false
    })
  },
  onAddParamsShow() {
 
    this.setData({
      addParamsShow: true
    })
  },
  onAddParamsSubmit(event) {
    _add({
      adjustmentGlazeTypeId: this.data._parent['id'],
      ...event.detail}, () => {
       this.fetchData()
    })
    this.onAddParamsCancel();
  },
  fetchParamTypeList(ids) {
 
  },
  onParamEdit(event) {
    const {
      item,
      data
    } = event.currentTarget.dataset;
    const _optionsInstance = this.selectComponent("#_options");
    _optionsInstance?.open(item, data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 
    try {
      this.setData({
        _parent: JSON.parse(decodeURIComponent(options.data))
      })
      
      this.fetchData();
    } catch (e) {
      wx.navigateBack()
    } 
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})